<?php

// shamlessly ganked from bas_thickbox 0.4

// -------------------------------------------------------------
function dub_thickbox($atts, $thing)
{
	global $prefs, $pretext, $thisarticle, $img_dir;
	
	extract(lAtts(array(
		'img'   => '',
		'cat' => '',
		'author' => '',
		
		'label'   => (empty($prefs['bas_thickbox_label']))?'':$prefs['bas_thickbox_label'],
		'labeltag' => (empty($prefs['bas_thickbox_labeltag']))?'':$prefs['bas_thickbox_labeltag'],
		'wraptag' => (empty($prefs['bas_thickbox_wraptag']))?'':$prefs['bas_thickbox_wraptag'],
		'wrapclass'    => (empty($prefs['bas_thickbox_wrapclass']))?'':$prefs['bas_thickbox_wrapclass'],
		'style'    => '',
		'break'   => (empty($prefs['bas_thickbox_break']))?'':$prefs['bas_thickbox_break'],
		'breakclass'   => (empty($prefs['bas_thickbox_breakclass']))?'':$prefs['bas_thickbox_breakclass'],

		'use_section' => (empty($prefs['bas_thickbox_use_section']))?'false':$prefs['bas_thickbox_use_section'],
		'use_cat' => (empty($prefs['bas_thickbox_use_cat']))?'false':$prefs['bas_thickbox_use_cat'],
		'use_author' => (empty($prefs['bas_thickbox_use_author']))? 'false' : $prefs['bas_thickbox_use_author'],
		
		'caption' => (empty($prefs['bas_thickbox_caption']))?'alt':$prefs['bas_thickbox_caption'],
	),$atts));

//	if(empty($prefs["dub_thickbox_install"]))
//		return '';

	//checking article images
	if(!empty($thisarticle['article_image']))
		$getImgs = split(',',$thisarticle['article_image']);

	//checking custom field "thickbox"
	if(!empty($thisarticle['thickbox']))
		$getImgs = safe_column('id', 'txp_image','category="'.$thisarticle['thickbox'].'"');

	//checking categorie for an img cat
	if($use_cat == 'true' && !empty($pretext['c'])){
		$getImgs = safe_column('id', 'txp_image','category="'.$pretext['c'].'"');
	}
	
	//checking section for an img cat
	if($use_section == 'true' && !empty($pretext['s'])){
		$getImgs = safe_column('id', 'txp_image','category="'.$pretext['s'].'"');
	}
	
	//checking tag attr "img"
	if(!empty($img))
		$getImgs = split(',',$img);

	//checking tag attr "cat"
	if(!empty($cat))
		$getImgs = safe_column('id', 'txp_image','category="'.$cat.'"');

	//checking "author"
//	print '<pre>'.print_r($pretext, true).'</pre>';
	if( !empty($use_author) && empty($author) ) {
		$author = $pretext['author'];
	}
	if( !empty($author) ) {
//		print "author: $author <br />\n";
		$getImgs = safe_column('id', 'txp_image','author="'.$author.'"');
	}
	
	
	if(empty($getImgs))
		return '';
		

	if(count($getImgs) == 1){
		$thumb = thumbnail(Array('id' => $getImgs[0]));
		$big = safe_row('id, ext,'.$caption,'txp_image','id ="'.$getImgs[0].'"');
		$out[] = '<a href="'.hu.$img_dir.'/'.$big['id'].$big['ext'].'" class="thickbox" title="'.$big[$caption].'">'.$thumb.'</a>';

	}
	else{
		//generate an roadtrip id
		// FIXME redo this using static vars
		$prefs['dub_thickbox_roadtrip'] = (empty($prefs['dub_thickbox_roadtrip']))?1:$prefs['dub_thickbox_roadtrip']+1;
		$roadtrip = $prefs['dub_thickbox_roadtrip']; // stupid
		
		foreach($getImgs as $getImg){
			$thumb = thumbnail(Array('id' => $getImg));
			$big = safe_row('id, ext,'.$caption,'txp_image','id ="'.$getImg.'"');
//			$out[] = '<a href="'.hu.$img_dir.'/'.$big['id'].$big['ext'].'" class="thickbox['.$prefs['dub_thickbox_roadtrip'].']" title="'.$big[$caption].'">'.$thumb.'</a>';
			$out[] = '<a href="'.hu.$img_dir.'/'.$big['id'].$big['ext'].'" class="thickbox" '.($roadtrip ? 'rel="'.$roadtrip.'"' : '').' title="'.$big[$caption].'">'.$thumb.'</a>';
		}
	}
	if(!empty($style))
		$style = " style='$style'";

	return  doLabel($label, $labeltag).doWrap($out, $wraptag, $break, $wrapclass,$breakclass,$style);

}


//--------------------------------------------------------------------------
function dub_thickbox_head()
{
	global $prefs;
	$prototype = fetch('filename','txp_file','id',$prefs["dub_thickbox_dub_lb04_prototype_js"]);
	$scriptaculous = fetch('filename','txp_file','id',$prefs["dub_thickbox_dub_lb04_scriptaculous_js"]);
	$thickbox = fetch('filename','txp_file','id',$prefs["dub_thickbox_dub_lb04_thickbox_js"]);
	$css = fetch('filename','txp_file','id',$prefs["dub_thickbox_dub_lb04_thickbox_css"]);
	$out[] = '<script type="text/javascript" src="'.hu.'files/$prototype"></script>';
	$out[] = '<script type="text/javascript" src="'.hu.'files/$scriptaculous?load=effects"></script>';
	$out[] = '<script type="text/javascript" src="'.hu.'files/$thickbox"></script>';
	$out[] = '<link rel="stylesheet" href="'.hu.'files/$css" type="text/css" media="screen" />';

	return join(n,$out);
}


//--------------------------------------------------------------------------
// Blugin's backend
//--------------------------------------------------------------------------
if (@txpinterface == 'admin')
{
	global $event;
	if($event == 'dub_thickbox_admin')
		dub_thickbox_text();
	add_privs('dub_thickbox_admin', '1');
	add_privs('dub_thickbox_custom_field', '1');
	register_tab("extensions", 'dub_thickbox_admin', 'dub thickbox');
	register_callback('dub_thickbox_admin',"dub_thickbox_admin");
	register_callback('dub_thickbox_custom_field','article', '', 1);
}

//--------------------------------------------------------------------------
global $custom_field;
$custom_field = '';
function dub_thickbox_custom_field()
{
	global $custom_field;
	$custom_field = safe_row('name', 'txp_prefs', 'val="thickbox" AND event ="custom"');
	if(!empty($custom_field)){
		$custom_field = str_replace('_set','',$custom_field['name']);
		ob_start('dub_thickbox_custom_cat');
	}
}

//--------------------------------------------------------------------------
function dub_thickbox_custom_cat($buffer){
	global $custom_field;
	$find = '=\<input type\="text" name\="'.$custom_field.'" value\="(.*?)" class\="edit" /\>=';

	//find saved value
	preg_match($find, $buffer, $hit);

	$img_cats = safe_rows('name,title','txp_category','type = "image"');
	$img_select = array();
	foreach($img_cats as $cat){
		extract($cat);
		$img_select[$name] = $title;
	}
	$replace = selectInput($custom_field, $img_select, $hit[1], 'true');
	return preg_replace($find,$replace, $buffer);

}

//--------------------------------------------------------------------------
function dub_thickbox_admin($event,$step)
{
	global $prefs;
	if(empty($prefs["dub_thickbox_install"])){

		if(!$step or !function_exists('dub_thickbox_install_'.$step) or !in_array($step, array('step1','step2','step3','step4',)))
		{
			echo dub_thickbox_install_step1();
		}
		else{
			$funct = 'dub_thickbox_install_'.$step;
			echo $funct();
		}
	}
	elseif(in_array($step,array('uninstall_step1','uninstall_step2'))){
		$funct = 'dub_thickbox_'.$step;
		echo $funct();
	}
	else{
		if(in_array($step,array('prefs','prefs_save'))){
			$funct = 'dub_thickbox_'.$step;
			echo $funct();
		}
		else
			echo dub_thickbox_prefs();
	}

}


global $vars;
$vars = array('dub_thickbox_use_section','dub_thickbox_use_cat','dub_thickbox_caption','dub_thickbox_label','dub_thickbox_labeltag','dub_thickbox_wraptag','dub_thickbox_wrapclass','dub_thickbox_break','dub_thickbox_breakclass');

//--------------------------------------------------------------------------
function dub_thickbox_prefs(){
	global $prefs,$vars;
	pagetop('bas thickbox',gTxt('dub_lb_admin'));
	$out[] = startTable( 'list' );
	$out[] = tr(
			tda(tag(gTxt('dub_lb_admin'),'h1'),' colspan="2"')
		);
	foreach($vars as $var){
		$out[] = tr(td(str_replace('dub_thickbox_','',$var)).td(fInput('text',$var, $prefs[$var],'edit')));
	}

	$out[] = tr(
			tda(
				fInput( "submit", '', gTxt( 'dub_lb_admin_save' ), "publish" ).
				eInput( 'dub_thickbox_admin' ).
				sInput('prefs_save')
			,' colspan="2" style="text-align:center;"')
		);
	$out[] = endTable();
	$out[] = dub_thickbox_uninstall_start();
	return form(join(n,$out));
}

//--------------------------------------------------------------------------
function dub_thickbox_prefs_save(){
	global $vars,$prefs;
	extract(gpsa($vars));
	$changes = false;
	foreach($vars as $var){
		if($prefs[$var] != $$var)
		{
			$q = safe_update("txp_prefs","val = '".$$var."'","name = '$var'");
			$changes = true;
		}
	}
	if($changes == true)
		$prefs = get_prefs();
	return dub_thickbox_prefs();
}

//--------------------------------------------------------------------------
function dub_thickbox_install_step1()
{
	pagetop('bas thickbox',gTxt('dub_lb_install').' - '.gTxt('dub_lb_in_step1'));
	$out[] = startTable( 'edit' );
	$out[] = tr(
			td(
				gTxt('dub_lb_in_step1_text').
				fInput( "submit", '', gTxt( 'dub_lb_in_start' ), "publish" ).
				eInput( 'dub_thickbox_admin' ).
				sInput('step2')
			)
		);
	$out[] = endTable( 'edit' );
	return form(join(n,$out));
}

//--------------------------------------------------------------------------
function dub_thickbox_install_step2()
{
	global $prefs;
	$import = gps('start_import');
	if(empty($import))
	{
		pagetop('bas thickbox',gTxt('dub_lb_install').' - '.gTxt('dub_lb_in_step2'));
		$out[] = startTable( 'edit' );
		$out[] = tr(
				td(
					gTxt('dub_lb_in_step2_start').
					form(
						fInput( "submit", '', gTxt( 'dub_lb_in_start_import' ), "publish" ).
						eInput( 'dub_thickbox_admin' ).
						sInput('step2').
						'<input type="hidden" name="start_import"value="true" />'
					)
				)
			);
		$out[] = tr(
				td(
					gTxt('dub_lb_in_step2_skip').
					form(
						fInput( "submit", '', gTxt( 'dub_lb_in_skip' ), "publish" ).
						eInput( 'dub_thickbox_admin' ).
						sInput('step3')
					)
				)
			);
		$out[] = endTable( 'edit' );

	}
	else{
		pagetop('bas thickbox',gTxt('dub_lb_install').' - '.gTxt('dub_lb_in_step2'));

		global $files,$images;

		if(!safe_column('id','txp_category','name ="dub_thickbox" AND type = "file"')){
			$q = safe_insert("txp_category", "name='dub_thickbox', title='dub_thickbox', type='file', parent='root'");
			rebuild_tree('root', 1, 'file');
		}
		$out[] = startTable( 'list' );
		$out[] = '<tr><td>';
		$out[] = gTxt('dub_lb_in_step2');
		$out[] = '<ul>';
		foreach($files as $file)
			$out[] = tag(dub_thickbox_import_file($file,'file'),'li');

		$out[] = '</ul>';
		$out[] = '<ul>';

		foreach($images as $img)
			$out[] = tag(dub_thickbox_import_file($img,'image'),'li');

		$out[] = '</ul>';
		$out[] = fInput( "submit", '', gTxt( 'dub_lb_in_next' ), "publish" );
		$out[] = eInput( 'dub_thickbox_admin' );
		$out[] = sInput('step3');
		$out[] = '</td></tr>';
		$out[] = endTable( 'edit' );
	}
	return form(join(n,$out));

}

//--------------------------------------------------------------------------
function dub_thickbox_install_step3()
{
	global $files,$images;
	pagetop('bas thickbox',gTxt('dub_lb_install').' - '.gTxt('dub_lb_in_step3'));
	$save = gps('save');
	if(empty($save))
	{
		$out[] = startTable( 'list' );
		$out[] = tr(
				tda(
					gTxt('dub_lb_in_step3_text'),' colspan="2"'
				)
			);

		$out[] = tr( td( '<b>Filename</b>') . td( '<b>txp id</b>'));

		foreach($files as $file)
		{
			extract($file);
			$out[] = tr( td( $filename) . td(fInput('text','dub_thickbox_'.str_replace('.','_',$filename), fetch('id','txp_file','filename',$filename),'edit')));
		}
		$out[] = tr( td( 'image name') . td( 'txp id'));

		foreach($images as $img)
		{
			extract($img);
				 $out[] = tr( td( $name) . td(fInput('text','dub_thickbox_'.str_replace('.','_',$name), fetch('id','txp_image','name',$name),'edit')));
		}

		$out[] = tr(
				td(
					fInput( "submit", '', gTxt( 'save' ), "publish" ).
					eInput( 'dub_thickbox_admin' ).
					sInput('step3').
					'<input type="hidden" name="save" value="true" />'
				)
			);

		$out[] = endTable();
		return form(join(n,$out));
	}
	else
	{
		$pos = 10;
		foreach($files as $file)
		{
			$file = $file['filename'];
			$value = gps('dub_thickbox_'.str_replace('.','_',$file));
			safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_".str_replace('.','_',$file)."',val = '".$value."',type = '2',event = 'dub_thickbox',html = 'text_input',position = '$pos'");
			$pos = $pos + 10;
		}
		foreach($images as $img)
		{
			$img = $img['name'];
			$value = gps('dub_thickbox_'.str_replace('.','_',$img));
			safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_".str_replace('.','_',$img)."',val = '".$value."',type = '2',event = 'dub_thickbox',html = 'text_input',position = '$pos'");
			$pos = $pos + 10;
		}



		$thickbox_css = join('',file("../files/dub_lb04_thickbox.css"));
		$thickbox_css = str_replace('../images/blank.gif',hu.'images/'.gps('dub_thickbox_dub_lb04_blank_gif').'.gif',$thickbox_css);
		$thickbox_css = str_replace('../images/prevlabel.gif',hu.'images/'.gps('dub_thickbox_dub_lb04_prevlabel_gif').'.gif',$thickbox_css);
		$thickbox_css = str_replace('../images/nextlabel.gif',hu.'images/'.gps('dub_thickbox_dub_lb04_nextlabel_gif').'.gif',$thickbox_css);
		$fp = fopen("../files/dub_lb04_thickbox.css","w");
		if ($fp){flock($fp,2);fputs ($fp, $thickbox_css);flock($fp,3);fclose($fp);}



		$thickbox_js = join('',file("../files/dub_lb04_thickbox.js"));
		$thickbox_js = str_replace('../images/loading.gif',hu.'images/'.gps('dub_thickbox_dub_lb04_loading_gif').'.gif',$thickbox_js);
		$thickbox_js = str_replace('../images/closelabel.gif',hu.'images/'.gps('dub_thickbox_dub_lb04_closelabel_gif').'.gif',$thickbox_js);
		$fp = fopen("../files/dub_lb04_thickbox.js","w");
		if ($fp){flock($fp,2);fputs ($fp, $thickbox_js);flock($fp,3);fclose($fp);}


		safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_use_section',val = 'false',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
		safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_use_cat',val = 'false',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
		safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_caption',val = 'caption',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
		safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_label',val = 'bas thickbox',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
		safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_labeltag',val = 'h2',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
		safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_wraptag',val = 'ul',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
		safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_wrapclass',val = 'thickbox',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
		safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_break',val = 'li',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
		safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_breakclass',val = '',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
		safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_install',val = 'true',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
		$out[] = startTable( 'edit' );
		$out[] = tr(
				td(
					gTxt('dub_lb_in_done').
					form(
						fInput( "submit", '', gTxt( 'done' ), "publish" ).
						eInput( 'dub_thickbox_admin' )
					)
				)
			);
		$out[] = endTable();
		return join(n,$out);
	}


}

//--------------------------------------------------------------------------
function dub_thickbox_install_step4()
{
	pagetop('bas thickbox',gTxt('dub_lb_install').' - '.gTxt('dub_lb_step4'));

	safe_insert('txp_prefs',"prefs_id = '1',name = 'dub_thickbox_install',val = 'true',type = '2',event = 'dub_thickbox',html = 'text_input',position = ''");
	$out[] = startTable( 'edit' );
	$out[] = tr(
			td(
				gTxt('dub_lb_step4_text').
				form(
					fInput( "submit", '', gTxt( 'done' ), "publish" ).
					eInput( 'dub_thickbox_admin' )
				)
			)
		);
	$out[] = endTable();
	return join(n,$out);
}

//--------------------------------------------------------------------------
function dub_thickbox_uninstall_start()
{
	return '<div style="text-align:center;margin:50px;"><hr style="border:none;border-top:1px solid grey" /><a href="?event=dub_thickbox_admin&amp;step=uninstall_step1">Uninstall bas thickbox</a></div>';
}

//--------------------------------------------------------------------------
function dub_thickbox_uninstall_step1()
{
	pagetop('bas thickbox',gTxt('dub_lb_uninstall').' - '.gTxt('dub_lb_un_step1'));
	$out[] = startTable( 'edit' );
	$out[] = tr(
			td(
				gTxt('dub_lb_un_step1_text').
				form(
					fInput( "submit", '', gTxt( 'dub_lb_uninstall_start' ), "publish" ).
					eInput( 'dub_thickbox_admin' ).
					sInput('uninstall_step2')
				)
			)
		);
	$out[] = tr(
			td(
				form(
					fInput( "submit", '', gTxt( 'dub_lb_uninstall_abort' ), "publish" ).
					eInput( 'dub_thickbox_admin' )

				)
			)
		);
	$out[] = endTable( 'edit' );
	return join(n,$out);
}

//--------------------------------------------------------------------------
function dub_thickbox_uninstall_step2()
{
	global $files,$images;
	pagetop('bas thickbox',gTxt('dub_lb_uninstall').' - '.gTxt('dub_lb_un_step2'));
		$out[] = '<h1>You can now delete the plugin</h1>';
		$out[] = '<ul>';
		foreach($files as $file){
			$fileID = fetch('id','txp_file','filename',$file['filename']);
			$q = safe_delete("txp_file","id='$fileID'");
			$delete = unlink('../files/'.$file['filename']);
			$out[] = tag(gTxt('file').' "'.$file['filename'].'"' .gTxt('deleted'),'li');
		}
		foreach($images as $img){
			$imgID = fetch('id','txp_image','name',$img['name']);
			$q = safe_delete("txp_image","id='$imgID'");

			$delete = unlink('../images/'.$imgID.$img['ext']);
			$out[] = tag(gTxt('image').' "'.$imgID.$img['ext'].'"' .gTxt('deleted'),'li');
		}

		$q = safe_delete("txp_prefs","event='dub_thickbox'");
		$out[] = tag(gTxt('dub_lb_prefs_deleted'),'li');
		$q = safe_delete("txp_category","name='dub_thickbox'");
		$out[] = tag(gTxt('dub_lb_cat_deleted'),'li');
		$out[] = '</ul>';
		$out[] = tag(gTxt('dub_lb_remove'),'p');
		return '<div style="margin:50px;">'.join(n,$out).'</div>';
}

//--------------------------------------------------------------------------
function dub_thickbox_text()
{
	global $prefs;
	$actLang = str_replace('-','_',$prefs['language']);
	$langFunct = 'dub_thickbox_lang_'.$actLang;
	@include_plugin($langFunct);
	if(function_exists($langFunct)){
		$basTextarray = $langFunct();
		$checkHelp = fetch('help','txp_plugin','name','dub_thickbox');
		if(!preg_match("=\<\!\-\- Lang\:$actLang \-\-\>=",$checkHelp)){
				$langFunct('updateDB');
		}
	}
	else{
		$basTextarray = dub_thickbox_lang_en_gb();
		$checkHelp = fetch('help','txp_plugin','name','dub_thickbox');
		if(!preg_match("=\<\!\-\- Lang\:en_gb \-\-\>=",$checkHelp)){
				dub_thickbox_lang_en_gb('updateDB');

		}

	}
	foreach($basTextarray as $key => $value){
		$GLOBALS['textarray'][$key] = $value;
	}
}

//--------------------------------------------------------------------------
global $fileURL;
$fileURL = 'http://samisdat.org/public-files/thickbox04';


global $files;
$files[] = array('filename'=>'dub_lb04_prototype.js','category'=>'dub_thickbox','description' => 'This file is used by bas thickbox.<br/> Prototype by Sam Stephenson.<br/> Visit http://prototype.conio.net/');
$files[] = array('filename'=>'dub_lb04_scriptaculous.js','category'=>'dub_thickbox','description' => 'This file is used by bas thickbox.<br/> Scriptaculous by Thomas Fuchs.<br/> Visit http://script.aculo.us/');
$files[] = array('filename'=>'dub_lb04_effects.js','category'=>'dub_thickbox','description' => 'This file is used by bas thickbox.<br/> Scriptaculous by Thomas Fuchs.<br/> Visit http://script.aculo.us/');
$files[] = array('filename'=>'dub_lb04_thickbox.js','category'=>'dub_thickbox','description' => 'This file is used by bas thickbox.<br/> thickbox by Lokesh Dhakar. <br/> Visit http://www.huddletogether.com/projects/thickbox2/');
$files[] = array('filename'=>'dub_lb04_thickbox.css','category'=>'dub_thickbox','description' => 'This file is used by bas thickbox.<br/> thickbox by Lokesh Dhakar.<br/> Visit http://www.huddletogether.com/projects/thickbox2/');

global $images;
$images[] = array('name'=>'dub_lb04_blank.gif','ext'=>'.gif','w'=>'1','h'=>'1','category'=>'dub_thickbox','caption'=>'This image is used by bas thickbox');
$images[] = array('name'=>'dub_lb04_close.gif','ext'=>'.gif','w'=>'26','h'=>'26','category'=>'dub_thickbox','caption'=>'This image is used by bas thickbox');
$images[] = array('name'=>'dub_lb04_closelabel.gif','ext'=>'.gif','w'=>'66','h'=>'22','category'=>'dub_thickbox','caption'=>'This image is used by bas thickbox');
$images[] = array('name'=>'dub_lb04_loading.gif','ext'=>'.gif','w'=>'32','h'=>'32','category'=>'dub_thickbox','caption'=>'This image is used by bas thickbox');
$images[] = array('name'=>'dub_lb04_next.gif','ext'=>'.gif','w'=>'45','h'=>'45','category'=>'dub_thickbox','caption'=>'This image is used by bas thickbox');
$images[] = array('name'=>'dub_lb04_nextlabel.gif','ext'=>'.gif','w'=>'63','h'=>'32','category'=>'dub_thickbox','caption'=>'This image is used by bas thickbox');
$images[] = array('name'=>'dub_lb04_prev.gif','ext'=>'.gif','w'=>'45','h'=>'45','category'=>'dub_thickbox','caption'=>'This image is used by bas thickbox');
$images[] = array('name'=>'dub_lb04_prevlabel.gif','ext'=>'.gif','w'=>'63','h'=>'32','category'=>'dub_thickbox','caption'=>'This image is used by bas thickbox');


//--------------------------------------------------------------------------
function dub_thickbox_import_file($file,$type)
{
	global $fileURL;
	extract($file);
	foreach($file as $key => $value)
	{
		$build_query [] = "$key = '$value'";
	}
	$build_query = join(',',$build_query);

	if($type == 'file')
	{
		if(!file_exists("../files/$filename"))
		{
			if(@copy("$fileURL/$filename", "../files/$filename"))
			{
				$query = safe_insert("txp_$type",$build_query);
				return tag($filename,'b').gTxt('dub_lb_in_file_imported');
			}
			else
				return tag($filename,'b').gTxt('dub_lb_in_file_import_error');;
		}
		else{
			return tag($filename,'b').gTxt('dub_lb_in_file_alreayd_exist');
		}

	}
	if($type == 'image'){
		if(!fetch('id','txp_image','name',$name)){
			if(@copy("$fileURL/$name", "../images/$name"))
			{
				$query = safe_insert("txp_$type",$build_query);
				$id = mysql_insert_id();
				rename("../images/$name", "../images/$id$ext");
				return tag($name,'b').gTxt('dub_lb_in_file_imported');
			}
			else
				return tag($name,'b').gTxt('dub_lb_in_file_import_error');;
		}
		else
			return tag($name,'b').gTxt('dub_lb_in_file_alreayd_exist');
	}
}

//--------------------------------------------------------------------------
function dub_thickbox_lang_en_gb($action = 'textarray')
{
	$basLang['textarray']['dub_lb_admin'] = 'thickbox preferences';
	$basLang['textarray']['dub_lb_admin_save'] = 'Save preferences';
	$basLang['textarray']['dub_lb_install'] = 'Install';
	$basLang['textarray']['dub_lb_in_step1'] = 'Start';
	$basLang['textarray']['dub_lb_in_step1_text'] = '
						<h2>Advanced Install of bas thickbox</h2>
						<p>This script leads you though the advaced install of bas thickbox.</p>
						<p>Following actions will be done:</p>
						<ul>
							<li>Import images and files.</li>
							<li>Configure the .css and .js files.</li>
							<li>Add plugin\'s preferences to txp.</li>
							<li>Configure plugin</li>
						</ul>
						<p>Make sure that a internet connection is open to importing files and images.</p>
						<p>Of course there is a uninstall script, if you want to remove the plugin later.</p>
						';

	$basLang['textarray']['dub_lb_in_start'] = 'Start advanded install';
	$basLang['textarray']['dub_lb_in_next'] = 'Next Step';
	$basLang['textarray']['dub_lb_in_step2'] = 'Importing Files';
	$basLang['textarray']['dub_lb_in_step2_start'] = '
						<h1>Importing images and files</h1>
						<p>You can import all needed files and images automaticly by clicking "Import".</p>
						<p>The files will be copied from samisdat.org to your txp-system.</p>
						<p>Make sure that you are connected to the internet.</p>
						';
	$basLang['textarray']['dub_lb_in_step2_skip'] = '
						<h1>Skip importing</h1>
						<p>If this import function fails or you want to upload to files manual, download</p>
						<p><a href="http://samisdat.org/public-files/thickbox04/ba_thickbox.zip">bas_thickbox.zip</a></p>
						<p>Upload all images with txp-image managment and all .js and .css file by txp file managment.</p>
						';
	$basLang['textarray']['dub_lb_in_start_import'] = 'Start import';
	$basLang['textarray']['dub_lb_in_skip'] = 'Skip Import';
	$basLang['textarray']['dub_lb_in_file_imported'] = ' has been imported.';
	$basLang['textarray']['dub_lb_in_file_import_error']  = ' import faild.';
	$basLang['textarray']['dub_lb_in_file_alreayd_exist'] = ' already exists.';
	$basLang['textarray']['dub_lb_in_step3'] = 'Configure';
	$basLang['textarray']['dub_lb_in_step3_text'] = '
						<h1>Configure plugins preferences</h1>
						<p>In this step all file and images id\'s will be saved to db.</p>
						<p>If you skiped the automatic import, make sure, that you uploaded all files and images.</p>
						<p>The javascript and CSS files will be updated to fit to your txp.</p>
						';
	$basLang['textarray']['dub_lb_in_done'] = '
						<h1>Installation completed</h1>
						<p>You can use dub_thickbox now:</p>
						<p>Include <code>&lt;txp:dub_thickbox_head /></code> into your head.</p>
						<p>I would recommon that you only include it to sections where you use thickbox, because the scripts not small.</p>
						<p>Use <code>&lt;txp:dub_thickbox /></code> in your article or article forms.</p>
						<p>Use <code>&lt;txp:dub_thickbox use_section="true"/></code> or <code>&lt;txp:dub_thickbox use_cat="true"/></code> pages or forms.</p>
						<p>On the next screen you can config the plugin to make it fitting tp your proposals.</p>
						<p>For more informations read the <a href="?event=plugin&step=plugin_help&name=dub_thickbox">plugin\'s help</a> or visit <a href="http://lab.samisdat.org/bas-thickbox">samisdat.org</a>.</p>
						';
	$basLang['textarray']['dub_lb_uninstall'] = 'Uninstall';
	$basLang['textarray']['dub_lb_un_step1'] = 'Start';
	$basLang['textarray']['dub_lb_un_step2'] = 'Removing all files and prefs';
	$basLang['textarray']['dub_lb_un_step1_text'] = '
						<h1>Uninstall bas thickbox</h1>
						<p>This script will remove bas thickbox and you can delete it at plugins tab.</p>
						<p>Following actions will be done:</p>
						<ul>
							<li>Delete all images and files wjich where imported during the advanced install.</li>
							<li>Removing all plugin prefs from txp.</li>
						</ul>
						<p>Think twice, before you start or abort.</p>
						';
	$basLang['textarray']['dub_lb_uninstall_start'] = 'Start uninstall';
	$basLang['textarray']['dub_lb_uninstall_abort'] = 'Abort uninstall';
	$basLang['textarray']['dub_lb_un_step2'] = 'Step 2';
	$basLang['textarray']['dub_lb_prefs_deleted'] = 'All plugin preferences have been removed from db';
	$basLang['textarray']['dub_lb_cat_deleted'] = 'The file cat dub_thickbox has been deleted';
	$basLang['textarray']['dub_lb_remove'] = 'You can now delete the plugin at <a href="?event=plugin">plugins tab</a>.';

	$basLang['db']['description'] = 'This plugin brings the <a href="http://www.huddletogether.com/projects/thickbox2/">thickbox JS v2.0</a> by <a href="http://www.huddletogether.com/">Lokesh Dhakar"</a> to textpattern. thickbox JS is a simple, unobtrusive script used to to overlay images on the current page.';
	$basLang['db']['help'] = '<!-- Lang:en_gb -->
					<h1><strong>bas thickbox</strong></h1>
					<p>by <a href="http://samisdat.org">Bastian Sackermann</a></p>
					<p>This plugin brings the <a href="http://www.huddletogether.com/projects/thickbox2/">thickbox JS</a> by <a href="http://www.huddletogether.com/">Lokesh Dhakar</a> to textpattern.<br />
					thickbox JS is a simple, unobtrusive script used to to overlay images on the current page.</p>
					<p>thickbox uses <a href="http://prototype.conio.net/">Prototype</a> by <a href="http://conio.net/">Sam Stephenson</a> and <a href="http://script.aculo.us/">Scriptaculous</a> by <a href="http://mir.aculo.us/">Thomas Fuchs</a>.</p>

					<h2>Installation</h2>
					<p>Before you can use this plugin, you have to run the advanced install at <em>extension</em> -> <strong><em>bas thickbox</strong></em>.<br />
					The script imports all files and images you need for thickboxing from <a href="http://samisdat.org">samisdat.org.</a></p>
					<p>You can skip the automatic import and download the files as <a href="http://samisdat.org/public-files/thickbox04/dub_thickbox.zip">dub_thickbox.zip</a> and upload the manual.<br />
					Use the txp image-managment for images and file-managment for files. Don`t rename the files.</p>
					<p>If you want to use a custom-field to refer the image category, just rename an custom-field to <strong>thickbox</strong>.<br />
					You can rename custom-field at admin -> advanced preferences</p>

					<h2>Localisation</h2>
					<p>The default language of <strong>bas Lighbox</strong> is English, but there are some translations.<br />
					For every supported language there is an additional plugin:</p>
					<ul>
						<li><a href="http://samisdat.org/files/dub_thickbox_lang_nl_nl.txt">Dutch: dub_thickbox_lang_nl_nl</a> translated by <a href="http://serepo.com">Sebastiaan Pouyet</a></li>
						<li><a href="http://samisdat.org/files/dub_thickbox_lang_de_de.txt">German: dub_thickbox_lang_de_de</a> translated by <a href="http://samisdat.org">Bastian Sackerman</a></li>
						<li><a href="http://samisdat.org/files/dub_thickbox_lang_pt_pt.txt">Portuguese: dub_thickbox_lang_pt_pt</a> translated by <a href="http://www.papeldigital.pt">Rui Pinto</a></li>
					</ul>
					<p>Download the language plugin, install and activate it.<br />
					<strong>bas thickbox</strong> will use this language plugin, if you active this language at <em>admin</em> -> <em>manage language</em>.</p>

					<h2>Plugin Perferences</h2>
					<p>After you run the advanced install, you can save some plugin preferences.<br />
					The saved prefs are used as default values and can be overwritten by including the attribute intag plugin&#8217;s tag.</p>

					<h2>Usage</h2>
					<p>Please <code>&lt;txp:dub_thickbox_head /&gt;</code> into your sites head, this tag will add all script and css to your site.<br />
					The thickbox scripts are 124 kB big, so include it only to sections where you want to use it.</p>

					<h2>Usage in article&#8217;s body or excerpt or in article-form</h2>
					<p>Include <code>&lt;txp:dub_thickbox /&gt;</code> to an article, you can include it to body and/or excerpt.</p>
					<p>You can refer the images within the tag by atrributes <strong>img</strong> or <strong>cat</strong></p>
					<ul>
						<li><code>&lt;txp:dub_thickbox img="2,3,7"/&gt;</code></li>
						<li><code>&lt;txp:dub_thickbox cat="a-cat"/&gt;</code></li>
					</ul>
					<p>Or you include just include <code>&lt;txp:dub_thickbox /&gt;</code> into article and refer images:</p>
					<ul>
						<li>Comma seperated in article&#8217;s image field</li>
						<li>Rename a custom-field to <strong>thickbox</strong>. This custom-field will be replaced by a drop-down list of all image categories. Select a cat from this list.</li>
					</ul>
					<h2>Usage in page or forms</h2>
					<p>You can include <code>&lt;txp:dub_thickbox /&gt;</code> into page, too.<br />
					For example place it into your sirebar and show your thumbs there.</p>
					<p>In page there is only a indirect way to refer the images:</p>
					<ul>
						<li><code>&lt;txp:dub_thickbox use_section="true"/&gt;</code> This will show all thumbs from the category with the very same name as the actual section. For example, if you want to show some images at section &#8220;about&#8221;, add all these images to img-cat &#8220;about&#8221;. Remember that section&#8217;s name and section&#8217;s itle must be the same.</li>
						<li><code>&lt;txp:dub_thickbox use_cat="true"/&gt;</code> This will show all thumbs from the category with the very same name as the actual category.</li>
					</ul>

					<h2>Plugins Attributes</h2>
					<p>You can customise the plugin by including attributes to tag, you can edit the default values at <em>extension</em> -> <em>bas thickbox</em></p>
					<ul>
						<li><code>&lt;txp:dub_thickbox caption="name|alt|caption" /&gt;</code> Use caption to define what text is displayed in the thickbox. Choose from <strong>name</strong>, <strong>alt</strong> or <strong>caption</strong></li>
						<li><code>&lt;txp:dub_thickbox label="My pics" labeltag="h2" /&gt;</code> Define a label</li>
						<li><code>&lt;txp:dub_thickbox wraptag="ul" break="li" /&gt;</code></li>
						<li><code>&lt;txp:dub_thickbox wraptag="ul" wrapclass="thumblist" break="li" breakclass="img" /&gt;</code> Asign classes to wraptag and break to style them by CSS.</li>
					</ul>
					';

	if($action == 'textarray')
		return $basLang['textarray'];
	if($action == 'updateDB'){
				safe_update("txp_plugin","help = '".$basLang['db']['help']."'","name='dub_thickbox'");
				safe_update("txp_plugin","description = '".$basLang['db']['description']."'","name='dub_thickbox'");

		}

}

?>